
# First stage: Hazard model with age, civil_unrest, and econ_crisis_3percent
survmod2 <- coxph(Surv(start, stop, gwf_fail) ~ age + civil_unrest + econ_crisis_3percent, data = ljimodern)
summary(survmod2)

# Generate hazard rates
ljimodern$hazard <- predict(survmod2, newdata = ljimodern, type = "risk")

library(stargazer)

stargazer(survmod2, 
          type = "latex",
          title = "Cox Proportional Hazards Model Results",
          covariate.labels = c("Leader's Age", "Civil Unrest", "Economic Crisis"),
          dep.var.labels = "Regime Failure",
          out = "cox_model_table.txt")

# Second stage
model2 <- lm(LJI ~ hazard + court_age + gdp_nl + gwf_party + gwf_personal + gwf_military + start, data = ljimodern)
summary(model2)

stargazer(model2, 
          title = "Regression Results",
          align = TRUE,
          dep.var.labels = c("Judicial Independence (LJI)"),
          covariate.labels = c("Hazard Rate", "Court Age", "GDP (log)", 
                               "Party Regime", "Personal Regime", "Military Regime", 
                               "Regime Start Year"),
          omit.stat = c("f", "ser"),
          type = "text")